<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/include/css/style.css" />

<div class="form-add">
		<form id="ajaxform"
			action="${pageContext.request.contextPath}/auth/object-active-add"
			style="background: #FDFDCF; padding: 5px; border: 1px solid #FDFDCF; border-radius: 5px;">
			<input name="urlId" type="hidden" value="${urlId}">
			<select name="objectId">
				<option value="">Select</option>
				<c:forEach items="${rolies}" var="obj">
					<option value="${obj.roleId}">${obj.roleDescription}</option>
				</c:forEach>
			</select>&nbsp;
			<input type="checkbox" name="view">Hiển thị&nbsp; 
			<input type="checkbox" name="insert">Thêm mới&nbsp;
			<input type="checkbox" name="update">Cập nhật&nbsp;
			<input type="checkbox" name="delete">Xóa&nbsp;
			<div style="clear: both; height: 5px"></div>
			<input type="submit" value="&nbsp;Lưu&nbsp;" style="height: 30px">
		</form>
	</div>

<c:if test="${!empty authAcepts}">
	
	
		<table class="tb-content" style="width: 100%">
			<tr>
				<th>Quyền </th><th>Hiển thị</th><th>Thêm mới</th><th>Cập nhật</th><th>Xóa</th><th>#</th>
			</tr>
			<c:forEach items="${authAcepts}" var="obj">
				<tr>
					<td>
						<a href="javascript:void(0)" 
							onclick="setValue('${obj.objectId}','${obj.activeView}','${obj.activeInsert}','${obj.activeUpdate}','${obj.activeDelete}')">${obj.objectName}</a>
					</td>
					<td style="text-align: center;"><c:if test="${obj.activeView > 0}">*</c:if></td>
					<td style="text-align: center;"><c:if test="${obj.activeInsert > 0}">*</c:if></td>
					<td style="text-align: center;"><c:if test="${obj.activeUpdate > 0}">*</c:if></td>
					<td style="text-align: center;"><c:if test="${obj.activeDelete > 0}">*</c:if></td>
					<td style="text-align: center;"><a href="javascript:void(0)" onclick="deleteOjb('${obj.id}')">Xóa quyền</a></td>
				</tr>
			</c:forEach>
		</table>
	
	
</c:if>


<script type="text/javascript">
function setValue(sl1,cb1,cb2,cb3,cb4){
	$('input[name=view]').removeAttr('checked');
	$('input[name=insert]').removeAttr('checked');
	$('input[name=update]').removeAttr('checked');
	$('input[name=delete]').removeAttr('checked');
	
	$('select[name=objectId]').val(sl1);
	
	if(cb1 == 1){
		$('input[name=view]').attr('checked','checked');
	}
	
	if(cb2 == 1){
		$('input[name=insert]').attr('checked','checked');
	}
	if(cb3 == 1){
		$('input[name=update]').attr('checked','checked');
	}
	if(cb4 == 1){
		$('input[name=delete]').attr('checked','checked');
	}
	
}
function deleteOjb(id){
	if(confirm('Ban chac chan muon xoa ?')){
		$.post('${pageContext.request.contextPath}/auth/object-active-delete', {
			id : id
		}, function(data) {
			$('a[id='+myClick+']').click();
		});
	}
}

$("#ajaxform").submit(function(e) {		
	var postData = $(this).serializeArray();
	var formURL = $(this).attr("action");
	$.ajax({
		url : formURL,
		type : "POST",
		data : postData,
		success : function(data, textStatus, jqXHR) {
			if(data != ''){
				alert(data);
			}			
			$('a[id='+myClick+']').click();
		},
		error : function(jqXHR, textStatus, errorThrown) {
			//if fails     
		}
	});
	e.preventDefault(); //STOP default action
	
});
	
</script>